package cn.sdormitory.violative.form;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.sdormitory.common.utils.ContainerUtil;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.smartclass.entity.ClassDv;
import cn.sdormitory.violative.entity.ViolationPatrol;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.Assert;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Data
public class ViolationPatrolReceiveForm {
    private Long violationPatrolId;
    private Integer timeSlot;
    private List<ViolationStudentInfo> violationStudentInfos;

    public List<ClassDv> generateClassViolations(ViolationPatrol violationPatrol) {
        String username = SysUserUtils.getSysUser().getUsername();
        String dvDate = DateUtil.formatDateTime(violationPatrol.getViolationTime());
        Date now = new Date();
        String dvType = generateDvType(violationPatrol.getViolationType());
        String timeSlotStr = Convert.toStr(timeSlot);
        String images = violationPatrol.getImages();
        Assert.notNull(images,"违纪巡查图片不能为空");
        String[] imageArray = images.split(",");
        return ContainerUtil.mapList(violationStudentInfos,info->{
            ClassDv classDv = new ClassDv();
            classDv.setStudentNo(info.getStudentNo());
            classDv.setStudentName(info.getStudentName());
            classDv.setClassId(info.getClassId());
            classDv.setCreate_by(username);
            classDv.setCreateTime(now);
            classDv.setDvDate(dvDate);
            classDv.setDvType(dvType);
            classDv.setTimeSlot(timeSlotStr);
            classDv.setDvDesc(info.getViolationContent());
            classDv.setViolationPatrolId(violationPatrolId);
            Long violationImageIndex = info.getViolationImageIndex();
            Assert.notNull(violationImageIndex,"违纪图片索引不能为空");
            Assert.isTrue(violationImageIndex < imageArray.length,"违纪图片索引超出范围");
            classDv.setImages(imageArray[violationImageIndex.intValue()]);
            return classDv;
        });
    }

    private String generateDvType(Integer violationType) {
        return Convert.toStr(violationType);
    }

    public void initTimeSlot(Date violationTime) {
        Assert.notNull(violationTime,"违纪时间不能为空,请检查");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(violationTime);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if(hour < 12){
            timeSlot = 1;
        }else if(hour < 18){
            timeSlot = 2;
        }else {
            timeSlot = 3;
        }
    }

    @Data
    public static class ViolationStudentInfo{
        private Long id;
        private Long classId;
        private String studentName;
        private String studentNo;
        private Long violationImageIndex;
        private String violationContent;
        public void validThrow(int imageSize){
            Assert.notNull(id,"学生编号不能为空");
            Assert.notNull(classId,"学生班级编号不能为空");
            Assert.notNull(studentName,"学生姓名不能为空");
            Assert.notNull(studentNo,"学生学号不能为空");
            Assert.notNull(violationImageIndex,"违纪图片索引不能为空");
            Assert.isTrue(violationImageIndex < imageSize,"违纪图片索引超出范围");
            Assert.notNull(violationContent,"违纪内容不能为空");
        }
    }

    public void validThrow(){
        Assert.notNull(violationPatrolId,"违纪巡查编号不能为空");
        Assert.notEmpty(violationStudentInfos,"违纪学生信息不能为空");
    }

    public void validStudentInfoItems(@NotNull ViolationPatrol violationPatrol){
        String images = violationPatrol.getImages();
        int imageSize = images.split(",").length;
        ContainerUtil.forEach(violationStudentInfos, item->item.validThrow(imageSize));
    }
}
